home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / sim.lha / sim / aux.h < prev    next >
C/C++ Source or Header  |  1992-08-12  |  7KB  |  167 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* aux.h */
  25.  
  26. #define FREE              0x0
  27. #define CS                0x1
  28. #define NUM               0x2
  29. #define LIST              0x3
  30. #define CS_TAG            0x1
  31. #define NUM_TAG           0x2
  32. #define LIST_TAG          0x3
  33. #define INT_TAG           0x6
  34. #define FLOAT_TAG         0x2
  35. #define LONGBUFF          65535
  36.  
  37. /******************************************************************************/
  38.  
  39. #define FOLLOW(op)        (*(LONG_PTR)(op))     /* return what op points to */
  40. #define TAG(op)           ((op) & 0x3)
  41. #define ISVAR(op)         (TAG(op) == FREE)
  42. #define ISCONSTR(op)      (TAG(op) == CS_TAG)
  43. #define ISNUM(op)         (TAG(op) == NUM_TAG)
  44. #define ISLIST(op)        (TAG(op) == LIST_TAG)
  45. #define ISINTEGER(op)     (((op) & 0x7) == INT_TAG)
  46. #define ISFLOAT(op)       (((op) & 0x7) == FLOAT_TAG)
  47. #define ISNONVAR(op)      (TAG(op) != FREE)     /* must have been derefed */
  48. #define ISFREE(variable)  ((LONG)variable == FOLLOW(variable))
  49. #define ISATOM(op)        (ISCONSTR(op) && (GET_STR_ARITY(op) == 0))
  50. #define ISNIL(op)         ((op) == nil_sym)
  51.  
  52. #define MAKEINT(op)       ((LONG)((((LONG)(op) << 3) | INT_TAG) & 0x7fffffff))
  53. #define MAKEADD(op)       ((LONG)((((LONG)(op) << 3) | INT_TAG) ))
  54. #define MAKENUM(op)       (floatp ? makefloat(op) : MAKEINT(op))
  55. #define INTVAL(op)        (((LONG)(op) << 1) >> 4)
  56. #define NUMVAL(op)        (ISINTEGER(op) ? INTVAL(op) : floatval(op))
  57. #define ADDVAL(op)        ((LONG)(((LONG)(op) >> 3)  & 0x1fffffff))
  58.  
  59. #define POS_OVERFLOW(op)  ((LONG)(op) & 0xf8000000)
  60. #define NEG_OVERFLOW(op)  (((LONG)(op) & 0xf8000000) != 0xf8000000)
  61. #define POS_ANS(op1,op2)  (((LONG)(op1)&0x40000000) == ((LONG)(op2)&0x40000000))
  62.  
  63. #define UNTAG(op)          ((op) &= 0xfffffffc) /* remove the tag from op */
  64. #define UNTAGGED(op)       ((op) & 0xfffffffc)  /* return val of op w/out tag */
  65.  
  66. #define GET_STR_PSC(op)    ((PSC_REC_PTR)(FOLLOW(UNTAGGED(op))))
  67. #define GET_STR_ARITY(op)  GET_ARITY(GET_STR_PSC(op))
  68. #define GET_STR_LENGTH(op) GET_LENGTH(GET_STR_PSC(op))
  69.  
  70. #define DEREF(op)        while (ISVAR(op)) {    \
  71.                     top = (LONG_PTR)op; \
  72.                             if (ISFREE(top))    \
  73.                    break;           \
  74.                             op = FOLLOW(top);   \
  75.                  }
  76.  
  77. #define NDEREF(op,labl)  top = (LONG_PTR)op;  \
  78.              if (!ISFREE(top)) {  \
  79.                 op = FOLLOW(top); \
  80.                 goto labl;        \
  81.              }
  82.  
  83. #define PUSHTRAIL(val)   if (((val) > (LONG)breg) || ((val) < (LONG)hbreg)) { \
  84.                 *trreg-- = val;                                   \
  85.                 if (trreg < tstack)                               \
  86.                    quit("Trail overflow\n");                      \
  87.              }
  88.  
  89. #define ENV_SIZE(op)     (*(BYTE_PTR)((LONG)(op) - 5))
  90.  
  91. #define BUFF_SIZE(ptr)   ( GET_LENGTH(ptr) == LONGBUFF ?                     \
  92.                            5 + ((*(LONG_PTR)(GET_NAME(ptr) - 4) + 3) >> 2) : \
  93.                            4 + ((GET_LENGTH(ptr) + 3) >> 2) )
  94.  
  95. /******************************************************************************/
  96. /* The following are macros for setting heap values. */
  97.  
  98. #define MAKE_FREE(type,variable)  (variable) = (type)&(variable)
  99. /* must pass a simple pointer, not an expression */
  100.  
  101. #define NEW_HEAP_FREE             MAKE_FREE(LONG, *hreg); hreg++
  102. /* make a free variable on the top of the heap */
  103.  
  104. #define NEW_HEAP_CON(x)           *hreg++ = ((x)|CS_TAG)
  105. /* make a new con node on the heap, with pointer value x, which can
  106.    be any untagged one word type */
  107.  
  108. #define NEW_HEAP_INT(val)         *hreg++ = MAKEINT(val)
  109. #define NEW_HEAP_FLOAT(val)       *hreg++ = val
  110. /* make a new num node on the heap, with value val */
  111.  
  112. #define NEW_HEAP_NODE(x)          *hreg++ = x
  113. /* make a new heap node with value x (one word type) */
  114.  
  115. /******************************************************************************/
  116.  
  117. /* ! ! ! ! IMPORTANT ! ! ! ! 
  118.  * The following macros are for accessing PSC record fields.  PLEASE use
  119.  * them other than actual field names so that we can change the data
  120.  * structure easily in the future !!!
  121.  *
  122.  * In the following macros, 'ptr' is typed PSC_REC_PTR (struct psc_rec *)
  123.  */
  124.  
  125. #define GET_ETYPE(ptr)  ((ptr)->entry_type)
  126. #define GET_ARITY(ptr)  ((ptr)->arity)
  127. #define GET_LENGTH(ptr) ((ptr)->length)
  128. #define GET_EP(ptr)     ((ptr)->ep)
  129. #define GET_NAME(ptr)   ((ptr)->nameptr)
  130.  
  131. #define IS_PRED(psc)    (GET_ETYPE(psc) == T_PRED)
  132. #define IS_DYNA(psc)    (GET_ETYPE(psc) == T_DYNA)
  133. #define IS_ORDI(psc)    (GET_ETYPE(psc) == T_ORDI)
  134. #define IS_BUFF(psc)    (GET_ETYPE(psc) == T_BUFF)
  135.  
  136. /* macro for computing indexing */
  137. #define GEN_SOT(opcode,arg1,arg2,arg3,ep) \
  138.    *ep++ = opcode;                        \
  139.    *ep++ = arg1;                          \
  140.    *((WORD_PTR)ep)++ = arg2;              \
  141.    *((WORD_PTR)ep)++ = arg3
  142.  
  143. #define GEN_TRY(opcode,arg1,arg2,ep) \
  144.    *ep++ = opcode;                   \
  145.    *ep++ = arg1;                     \
  146.    *(LONG_PTR)ep = arg2;             \
  147.    ep += 2
  148.  
  149. #define IHASH(val,size)  ((val & 0x3ffffffc) >> 2 + TAG(val)) % size
  150.  
  151. struct hrec {
  152.    LONG     l;
  153.    LONG_PTR link;
  154. };
  155.  
  156. struct hrec indextab[1024];
  157.  
  158. /******************************************************************************/
  159.  
  160. #define ERROR(arg) printf ("error %d\n",arg)
  161.  
  162. #define FAIL1      lpcreg = (WORD_PTR)*(breg + 1)
  163.  
  164. #define FAIL0      if (hitrace) printf("Fail\n"); pcreg = (WORD_PTR)*(breg + 1)
  165.  
  166. /******************************************************************************/
  167.